package com.bos.service.impl;

import com.bos.data.model.BosUserModel;
import com.bos.data.model.LogsModel;
import com.bos.data.repositories.LogRepository;
import com.bos.data.repositories.jpa.BosLogJPARepository;
import com.bos.mapper.LogsMapper;
import com.bos.service.LogsService;
import com.bos.common.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.Date;

@Service("logsServiceImpl")
public class LogsServiceImpl implements LogsService {
    private Logger logger = LoggerFactory.getLogger(LogsServiceImpl.class);
    @Autowired
    LogsMapper logMapper;

    @Autowired
    private BosLogJPARepository bosLogJPARepository;

	@Autowired
	private LogRepository logRepository;

    @Override
    public PageData findLogs(String leader, String logs, int pageSize, int pageIndex) {
		return logRepository.findLogs(leader,logs,pageSize,pageIndex);
    }


    @Transactional
    @Override
    public void insertLogs(HttpSession session, String message) {
        BosUserModel user = (BosUserModel) session.getAttribute("user");
        try {
            if (user != null) {
                LogsModel logsModel = new LogsModel();
                logsModel.setName(user.getName());
                logsModel.setLevel("info");
                logsModel.setMessage(message);
                logsModel.setUpdateTime(new Timestamp(new Date().getTime()));
                bosLogJPARepository.save(logsModel);
            }
        } catch (Exception e) {
            logger.error("添加日志报错:[user:{}]", user);
        }
    }
}
